package com.taiji.dicom4cloud.common.security.handler;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.taiji.dicom4cloud.common.core.constrant.CommonConstants;
import com.taiji.dicom4cloud.common.security.contrants.SecurityConstants;
import com.taiji.dicom4cloud.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/5/20
 **/
public class FieldFillHandler  implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (ObjectUtil.isNotNull(SecurityUtils.getUser())) {
            setFieldValByName(
                    SecurityConstants.CREATOR, SecurityUtils.getUser().getUserId(), metaObject);
            setFieldValByName(
                    SecurityConstants.CREATOR_NAME, SecurityUtils.getUser().getStaffName(), metaObject);
            setFieldValByName(
                    SecurityConstants.DATASTATUS, CommonConstants.DATASTATUSYES, metaObject);

            setFieldValByName(
                    SecurityConstants.CREATE_TIME, LocalDateTime.now(), metaObject);

            setFieldValByName(
                    SecurityConstants.ORG_ID, SecurityUtils.getUser().getOrgId(), metaObject);

            setFieldValByName(
                    SecurityConstants.ORG_NAME, SecurityUtils.getUser().getOrgName(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (ObjectUtil.isNotNull(SecurityUtils.getUser())) {
            setFieldValByName(
                    SecurityConstants.MODIFIER, SecurityUtils.getUser().getUserId(), metaObject);
            setFieldValByName(
                    SecurityConstants.MODIFY_NAME, SecurityUtils.getUser().getStaffName(), metaObject);
            setFieldValByName(
                    SecurityConstants.MODIFY_TIME, LocalDateTime.now(), metaObject);
        }
    }

}
